<?php
namespace App\HttpController\Pudu;

use App\Common\Controller\Rest;

class MemberCashes extends Rest
{
    public $modelName = 'MemberCash';   // model 类名称

    public function GETIndex()
    {
        // $this->param['userId']
        if(isset($this->param['Id'])){
            $res = $this->model()->getDataById($this->param['Id']); 
        }else{
            if(isset($this->param['shopMembers'])){
                $res = $this->model()->getChildrenDataList($this->param);
            }else{
                $res = $this->model()->getDataList($this->param);
            }
        }
        $this->result($res, ['msg' => self::$model->getError(), 'data' => $res]);
    }

    /**
     * 新增提现申请
     *
     * @return void
     */
    public function POSTIndex()
    {
        // TODO 判断日期 (只有周五能够申请体现)
        // $this->abort(date("w") == 5, ['msg' => '请在每周五申请提现!']);
        $time = '2018-07-31 23:59:59';
        $date = date('Y-m-d H:i:s');
        if($time >= $date){
            $this->json(['code'=>'400','msg'=>'提现功能暂停止7月31日','data'=>false]);
        }
        // TODO 获取需要插入的基本信息
        $Member = $this->model('Member');
        $memberInfo = $Member->get($this->param['userId']);
        // TODO 获取提现手续费
        $charge = $this->getCharge($this->param['much']);

        // TODO 检查余额是否充足
        $this->abort($memberInfo['balance'] >= ($this->param['much'] + $charge), ['msg' => '余额不足!']);

        $record = [
            'userId' => $this->param['userId'],
            'username' => $memberInfo['username'],
            'roleId' => $memberInfo['roleId'],
            'much'   => $this->param['much'],   // 提现金额
            'remark' => $this->param['remark'],  // 提现备注
            'bankNumber' => $memberInfo['bankNumber'],
            'bankName' => $memberInfo['bankName'],
            'bankAddress' => $memberInfo['bankAddress'],
            'cardholder' => $memberInfo['cardholder']
        ];
        // 暂时扣除现金钱包 提现的金额
        
        // TODO 生成记录
        $insertData[] = [
            'userId' => $this->param['userId'],
            'username' => $memberInfo['username'],
            'change' => -$this->param['much'],
            'remain' => $memberInfo['balance']-$this->param['much'],
            'type'   => 1,
            'remark' => '提现'
        ];

        if($charge != 0){ // 手续费为0 的情况 不生成记录
            $insertData[] = [
                'userId' => $this->param['userId'],
                'username' => $memberInfo['username'],
                'change' => -$charge,
                'remain' => $memberInfo['balance'] - $this->param['much'] - $charge,
                'type'   => 1,
                'remark' => '提现手续费'
            ];
        }

        $Member->startTrans();
        try{
            $Member->where(['id' => $this->param['userId']])->setDec('balance', $this->param['much'] + $charge);
            $this->model('MemberRecord')->insertAll($insertData);
            $res = $this->model()->createData($record);
            $Member->commit();
        }catch(\Exception $e){
            $Member->rollback();
            $this->error(['msg' => $e->getMessage()]);
        }

        $this->result($res, ['msg' => self::$model->getError()]);
    }


    /**
     * 获取手续费
     *
     * @param [type] $much
     * @return void
     */
    private function getCharge($much)
    {
        $chargePer = 0;
        $charge = $much * $chargePer;
        return $charge;
    }


    /**
     * 提现通过 和驳回
     *
     * @return void
     */
    public function PUTIndex()
    {
        $res = $this->model()->updateDataById($this->param, $this->param['Id']);
        $this->result($res, ['msg' => self::$model->getError()]);
    }
}
